package xFGE
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import mx.core.UIComponent;
	
	import xFGE.Interface.IGameMain;
	import xFGE.Loader.xLoader;
	import xFGE.Timer.xFPS;
	import xFGE.Timer.xTimer;
	import xFGE.debug.DebugConsole;
	import xFGE.event.EventQueue;
	import xFGE.event.InQueueEvent;
	import xFGE.manager.PanelManager;
	import xFGE.net.NetConnectionManager;
	import xFGE.xDisplay.xScene.xLayerObj;
	import xFGE.xDisplay.xScene.xSceneManger;
	
	public class FGE extends UIComponent
	{
		static private var _myInstance:FGE; 
		
		/** 游戏主函数 */
		private var _myGameManger:IGameMain;
		
		private var _loader:xLoader;
		
		private var _sceneManger:xSceneManger;
		
		private var _panelManager:PanelManager;
		
		private var _netConnectionManager:NetConnectionManager;
		
		private var _timer:xTimer;
		
		private var  _stateText:TextField
		
		private var _eventQueue:EventQueue;		
		
		private var _debugConsole:DebugConsole;
		
		public var debugMode:Boolean;
		
		public function FGE()
		{
			//TODO: implement function
			if(_myInstance)
				throw(new Error("请用GetInstance获取实例"));
			_xFGEInit();
		}
		/**
		 * 获取实例 
		 * @return 
		 * 
		 */
		static public function GetInstance():FGE
		{
			if(_myInstance == null)
				_myInstance = new FGE();
			return _myInstance;
		}
		
		/**
		 * 注册GameManger 
		 * @param myGameManger
		 * 
		 */
		public function RegGameManger(myGameManger:IGameMain):void
		{
			if(_myGameManger == null)
			_myGameManger = myGameManger;
		}
		
		/**
		 * 引擎开始 
		 * 
		 */
		public function RockOn():void
		{
				//初始化场景布局
			var sceneManger:xSceneManger = xSceneManger.getInstance();
			sceneManger.Init(this);
			//初始化FPS
			xFPS.getInstance().initFPS();
			//设置监听事件
			this.addEventListener(Event.ENTER_FRAME,_onEnterFrame);
		
			
			/* var mainScene:xSceneObj= sceneManger.currentScene;
			var stateLayer:xLayerObj = mainScene.CreateLayer("stateinfo",0,0);
			mainScene.SetAllwayTopMost("stateinfo"); */
			var stateLayer:xLayerObj = sceneManger.sysInfoLayer;
			//FPS
			_stateText= new TextField();
			_stateText.text = "FPS:0";
			_stateText.type = TextFieldType.DYNAMIC;
			_stateText.name = "stateiinfo"
			var format:TextFormat = _stateText.getTextFormat();
			format.size = 20;
			format.bold = true;
			format.font = "宋体";
			_stateText.setTextFormat(format);
			_stateText.defaultTextFormat =format;
			_stateText.textColor = 0xff0f0f0f;
			_stateText.mouseEnabled = false;
			_stateText.selectable = false;
			stateLayer.addChild(_stateText);
			stateLayer.mouseEnabled = false;
			
			_debugConsole = new DebugConsole();
			_debugConsole.initDebugTrace();
			stateLayer.addChild(_debugConsole);
			
			if(!_myGameManger)throw(new Error("请注册GameManger"));
			if(!_myGameManger.GameInit()) throw(new Error("游戏初始化失败"));
			if(stage == null ) throw (new Error("stage为null"));
			
			_netConnectionManager = new NetConnectionManager();
			_eventQueue = new EventQueue();
			//游戏开始
			_myGameManger.GameStart();
			
			//监听键盘事件
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyboard_handler);
		}
		
		public function keyboard_handler(event:KeyboardEvent):void
		{
			
			if(event.keyCode == 192)
			{
				trace("event.keyCode: "+event.keyCode);
				_debugConsole.switchShow();
			}
		}
		public function ShowFps(flag:Boolean):void
		{
				_stateText.visible = flag;
		}
		/**
		 * 每帧
		 * @param eve
		 * 
		 */
		private function _onEnterFrame( eve:Event):void
		{
			_myGameManger.GameLoop();
			
			_stateText.text = "FPS:" + xFPS.getInstance().fps;
		}
		/**
		 * 引擎初始化 
		 * @return 
		 * 
		 */
		private function _xFGEInit():Boolean
		{
			System.useCodePage = true;
			
			_loader = xLoader.getInstance();
			
			_sceneManger = xSceneManger.getInstance();
			
//			_netHelper = NetHelper.getInstance();
			
			_panelManager = PanelManager.GetInstance();
			
			return true;
		}
		
		public function get loader():xLoader
		{
			return _loader;
		}
		
		public function get sceneManger():xSceneManger
		{
			return _sceneManger;
		}
		
		public function get netConnectionManager():NetConnectionManager
		{
			return _netConnectionManager;
		}
		
		public function get panelManger():PanelManager
		{
			return _panelManager;
		}
		
		public function get eventQueue():EventQueue
		{
			return _eventQueue;
		}
		
		public function debugTrace(str:String,color:String = "#000000"):void
		{
			_debugConsole.debugTrace(str,color);
		}
		
		public function parse(dataObj:Object):void
		{
			try
			{
				var e:InQueueEvent = new InQueueEvent(dataObj.cmd);
				e._eventObject = dataObj;
				eventQueue.pushQueue(e);
			}
			catch(e:Error)
			{
				debugTrace("BGameConnection.parse: "+e);
			}
		}
		
	}
}